<template>
  <div class="binding-page">
    <!-- 1. 顶部个人信息卡片 -->
    <header class="profile-header">
      <div class="header-bg"></div>
      <div class="profile-card">
        <div class="profile-avatar">
          <span>{{ editableProfile.fullName?.charAt(0) }}</span>
        </div>
        <h2 class="name">{{ editableProfile.fullName || '暂无姓名' }}</h2>
      </div>
    </header>

    <main class="binding-content">
      <!-- 2. 选择家属 -->
      <van-cell-group inset title="选择家属">
        <van-field v-model="familyUserId" label="家属用户名:" placeholder="请输入家属用户名" />
        <van-button type="primary" size="mini" round @click="handleSearchFamily">搜索家属</van-button>
      </van-cell-group>

      <!-- 3. 绑定申请 -->
      <van-cell-group inset title="申请绑定家属">
        <van-button type="success" size="large" round @click="submitBindRequest">申请绑定</van-button>
      </van-cell-group>

      <!-- 4. 申请状态 -->
      <van-toast v-if="statusMessage" :message="statusMessage" type="success" />
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted} from 'vue';
import { showToast } from 'vant';
import { bindElderlyToFamily } from '@/api/elderly'; // 引入封装的API
import { useElderlyStore } from '@/stores/elderlyStore';

const elderlyStore = useElderlyStore();

// 可编辑的老人信息
const editableProfile = computed(() => elderlyStore.elderlyInfo);
const elderlyUserId = editableProfile.value.userId
// console.log(elderlyUserId)

// 家属用户ID和绑定申请的状态
const familyUserId = ref('');
const statusMessage = ref('');

// 提交绑定请求
const submitBindRequest = async () => {
  if (!familyUserId.value) {
    showToast('请先选择家属');
    return;
  }

  try {
    const result = await bindElderlyToFamily(familyUserId.value, elderlyUserId);
    statusMessage.value = `申请成功，等待审批。ID: ${result.id}`;
    showToast('申请绑定家属成功');
  } catch (error) {
    statusMessage.value = '申请失败，请重试';
    showToast('绑定失败');
  }
};

// 搜索家属用户
const handleSearchFamily = () => {
  if (!familyUserId.value) {
    showToast('请输入家属ID');
    return;
  }

  // TODO: 添加搜索家属的API逻辑
  showToast(`正在搜索家属ID: ${familyUserId.value}`);
};

</script>

<style scoped>
.binding-page {
  background-color: #f7f8fa;
  min-height: 100vh;
  padding-bottom: 20px;
}

/* 顶部美化 */
.profile-header {
  position: relative;
  height: 180px;
  margin-bottom: 80px; /* 为凸出的卡片留出空间 */
}
.header-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 150px;
  background: linear-gradient(135deg, #5c9cff 0%, #3a7afe 100%);
  border-bottom-left-radius: 20px;
  border-bottom-right-radius: 20px;
}
.profile-card {
  position: absolute;
  top: 60px;
  left: 16px;
  right: 16px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(120, 120, 120, 0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  padding-top: 50px; /* 为头像留出空间 */
}
.profile-avatar {
  position: absolute;
  top: -40px; /* 将头像向上移动一半 */
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-color: #3a7afe;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 40px;
  font-weight: bold;
  border: 4px solid #fff;
}
.profile-card .name {
  font-size: 22px;
  font-weight: bold;
  margin: 0 0 10px 0;
  color: #323233;
}

/* 主内容区域 */
.binding-content {
  padding: 16px;
}

/* 按钮样式 */
.van-button {
  margin: 20px 0;
}
</style>
